<html>
<head><meta charset="utf-8"><title>AtomicCell and padding · t-lang/wg-unsafe-code-guidelines · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/index.html">t-lang/wg-unsafe-code-guidelines</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html">AtomicCell and padding</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="168423842"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168423842" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168423842">(Jun 18 2019 at 17:19)</a>:</h4>
<blockquote>
<p>AtomicCell&lt;T&gt; could use ptr::write_fields to ensure that the padding bytes of the T stored inside it is always zero (or some other value)</p>
</blockquote>
<p>I don't see how that would work. After moving the <code>AtomicCell</code>, padding would be bad again.</p>



<a name="168426762"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168426762" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168426762">(Jun 18 2019 at 17:30)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> as mentioned, the <code>AtomicCell</code> does not store a <code>T</code>, it stores a <code>usize</code>, or <code> [u8; 16]</code>. <code>T</code> is only used in the API with its users.</p>



<a name="168426850"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168426850" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168426850">(Jun 18 2019 at 17:31)</a>:</h4>
<p><span class="user-mention" data-user-id="132920">@gnzlbg</span> but it's supposed to work for <em>any</em> <code>T</code></p>



<a name="168426904"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168426904" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168426904">(Jun 18 2019 at 17:32)</a>:</h4>
<p>so at the least you'd need enogh const generics to make it <code>[u8; mem::size_of::&lt;T&gt;()]</code></p>



<a name="168426906"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168426906" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168426906">(Jun 18 2019 at 17:32)</a>:</h4>
<p>how can it be atomic for any T ?</p>



<a name="168426930"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168426930" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168426930">(Jun 18 2019 at 17:32)</a>:</h4>
<p>once it becomes too large it switches to seqlocks</p>



<a name="168426943"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168426943" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168426943">(Jun 18 2019 at 17:32)</a>:</h4>
<p>I thought it only works for T that fit in actual hardware atomics and then it switches to locks</p>



<a name="168426963"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168426963" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168426963">(Jun 18 2019 at 17:32)</a>:</h4>
<p>but size of <code>AtomicCell&lt;T&gt;</code> must be the same as <code>T</code>, not wasting anything for small types and scaling to any type</p>



<a name="168426984"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168426984" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168426984">(Jun 18 2019 at 17:33)</a>:</h4>
<p>so if it switches to locks, the <code>compare_and_exchange</code> function does something completely different</p>



<a name="168427018"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168427018" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168427018">(Jun 18 2019 at 17:33)</a>:</h4>
<p>maybe have a look at the code :) <a href="https://github.com/crossbeam-rs/crossbeam/blob/master/crossbeam-utils/src/atomic/atomic_cell.rs" target="_blank" title="https://github.com/crossbeam-rs/crossbeam/blob/master/crossbeam-utils/src/atomic/atomic_cell.rs">https://github.com/crossbeam-rs/crossbeam/blob/master/crossbeam-utils/src/atomic/atomic_cell.rs</a></p>



<a name="168427037"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168427037" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168427037">(Jun 18 2019 at 17:33)</a>:</h4>
<p>yes but it still does something wrong because it compares undef</p>



<a name="168427117"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168427117" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168427117">(Jun 18 2019 at 17:34)</a>:</h4>
<p>Does it do a byte-wise comparison for the case in which T is too large ?</p>



<a name="168427123"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168427123" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168427123">(Jun 18 2019 at 17:34)</a>:</h4>
<p>but yes the underlying <code>compare_exchange</code> is only used for small types</p>



<a name="168427128"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168427128" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168427128">(Jun 18 2019 at 17:34)</a>:</h4>
<p>I thought it would just call PartialEq or whatever there</p>



<a name="168427174"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168427174" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168427174">(Jun 18 2019 at 17:34)</a>:</h4>
<p>it also does the bytewise thing, I forgot why</p>



<a name="168427246"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168427246" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168427246">(Jun 18 2019 at 17:35)</a>:</h4>
<p>no idea why that would be necessary, if you take a lock, you don't need to use a hardware CAS, and that also means you can just call PartialEq within the lock</p>



<a name="168427285"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168427285" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168427285">(Jun 18 2019 at 17:36)</a>:</h4>
<p>might be just an artifact of how they set up their framework</p>



<a name="168427335"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168427335" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168427335">(Jun 18 2019 at 17:36)</a>:</h4>
<p>with hardware CAS (for small types), you don't have a choice, you need to do a byte equality, and also you need to compare packing</p>



<a name="168427363"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168427363" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168427363">(Jun 18 2019 at 17:36)</a>:</h4>
<p>right now, it is impossible to avoid passing undef to the hardware CAS, because you don't have a generic way to "write" to the padding bytes of a type</p>



<a name="168427411"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168427411" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168427411">(Jun 18 2019 at 17:37)</a>:</h4>
<p>well a failing <code>compare_exchange</code> should also not do any writes</p>



<a name="168427427"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168427427" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168427427">(Jun 18 2019 at 17:37)</a>:</h4>
<p>if it could, we could just have an "atomic" variant of <code>freeze</code> and call it a day</p>



<a name="168427440"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168427440" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168427440">(Jun 18 2019 at 17:38)</a>:</h4>
<p>it wouldn't do any writes</p>



<a name="168427481"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168427481" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168427481">(Jun 18 2019 at 17:38)</a>:</h4>
<p>that's what I said earlier today about data race issues</p>



<a name="168437264"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168437264" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168437264">(Jun 18 2019 at 17:38)</a>:</h4>
<p>ah so you want to use <code>[u8; size_of::&lt;T&gt;()]</code> to protect mvoes of <code>AtomicCell</code> and then make all write methods it offers ensure there is no undef</p>



<a name="168437268"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168437268" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168437268">(Jun 18 2019 at 17:38)</a>:</h4>
<p>When you construct an <code>AtomicCell&lt;T&gt;</code>, by calling <code>AtomicCell::new(T)</code>, if <code>T</code> is small enough, you zero the <code>AtomicCell</code> storage, and only overwrite with the content of the input the bytes that are not packing via a <code>ptr::write_fields</code></p>



<a name="168437280"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168437280" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168437280">(Jun 18 2019 at 17:39)</a>:</h4>
<p>no need for a new operation then though. just use <code>freeze</code> on the value after you turned it into a byte array and before you stored it.</p>



<a name="168437293"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168437293" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168437293">(Jun 18 2019 at 17:39)</a>:</h4>
<p>the problem with freeze, is that you are not guaranteed to get the same indeterminate value every time</p>



<a name="168437296"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168437296" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168437296">(Jun 18 2019 at 17:39)</a>:</h4>
<p>IIUC</p>



<a name="168437302"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168437302" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168437302">(Jun 18 2019 at 17:39)</a>:</h4>
<p>while being able to write <code>0</code> to the padding bytes gives you that</p>



<a name="168437304"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168437304" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168437304">(Jun 18 2019 at 17:39)</a>:</h4>
<p>that should no matter. that's why atomic_exchange runs a loop anways.</p>



<a name="168437360"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168437360" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168437360">(Jun 18 2019 at 17:40)</a>:</h4>
<p>the issue with your proposal is figuring out where the padding bytes are^^ have fun doing that in a <code>union</code>...</p>



<a name="168437399"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168437399" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168437399">(Jun 18 2019 at 17:40)</a>:</h4>
<p>in <code>AtomicCell::compare_exchange(T)</code> you copy the input to the stack, zeroing padding, and then do the hardware CAS, and you are guaranteed that the padding bytes of the input and the value stored in the AtomicCell are the same</p>



<a name="168437425"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168437425" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168437425">(Jun 18 2019 at 17:41)</a>:</h4>
<blockquote>
<p>the issue with your proposal is figuring out where the padding bytes are^^ have fun doing that in a union</p>
</blockquote>
<p>unions have no padding AFAICT</p>



<a name="168437497"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168437497" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168437497">(Jun 18 2019 at 17:42)</a>:</h4>
<p>ah no, you are right, ofc they have</p>



<a name="168437525"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168437525" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168437525">(Jun 18 2019 at 17:42)</a>:</h4>
<p>but from the <code>AtomicCell&lt;Union&gt;</code> POV, you can't know which variant of the union is active, so you have to do a bytewise equality on the whole union</p>



<a name="168437529"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168437529" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168437529">(Jun 18 2019 at 17:42)</a>:</h4>
<p>i don't know how that can ever work properly</p>



<a name="168437530"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168437530" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168437530">(Jun 18 2019 at 17:42)</a>:</h4>
<blockquote>
<p>in <code>AtomicCell::compare_exchange(T)</code> you copy the input to the stack, zeroing padding, and then do the hardware CAS, and you are guaranteed that the padding bytes of the input and the value stored in the AtomicCell are the same</p>
</blockquote>
<p>what the implementation already does is, if the first CAS failed but the <code>eq</code> function said that the value <em>was</em> equal, to now continue with the value that was read the first time</p>



<a name="168437540"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168437540" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168437540">(Jun 18 2019 at 17:43)</a>:</h4>
<p>and we freeze only once in the beginning</p>



<a name="168437550"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168437550" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168437550">(Jun 18 2019 at 17:43)</a>:</h4>
<p>so even with padding, in the 2nd iteration we are definitely looking at the same bits</p>



<a name="168437556"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168437556" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168437556">(Jun 18 2019 at 17:43)</a>:</h4>
<p>it needs that loop anyway to handle the case where things with different bits are <code>eq</code></p>



<a name="168437623"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168437623" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168437623">(Jun 18 2019 at 17:44)</a>:</h4>
<p>so this scheme I am proposing works fine, I think, <em>if</em> we can ensure that the data in memory has no <code>Undef</code>, and assuming a (non-atomic) <code>freeze</code> operation</p>



<a name="168437664"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168437664" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168437664">(Jun 18 2019 at 17:44)</a>:</h4>
<p>IIUC the data in memory can only have <code>Undef</code> at padding bytes</p>



<a name="168437827"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168437827" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168437827">(Jun 18 2019 at 17:46)</a>:</h4>
<p>yes isn't that what we talked about for quite a while now? /confused</p>



<a name="168437900"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168437900" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168437900">(Jun 18 2019 at 17:47)</a>:</h4>
<p>yes, but I don't know how you can ensure that the date in memory has no undef</p>



<a name="168437921"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168437921" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168437921">(Jun 18 2019 at 17:48)</a>:</h4>
<p>your own scheme relies on that...?</p>



<a name="168437962"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168437962" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168437962">(Jun 18 2019 at 17:48)</a>:</h4>
<p>at least, without either a way to either <code>freeze</code> that data, or write to its padding</p>



<a name="168437981"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168437981" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168437981">(Jun 18 2019 at 17:48)</a>:</h4>
<p>I am so confused now</p>



<a name="168437988"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168437988" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168437988">(Jun 18 2019 at 17:48)</a>:</h4>
<p>ah ok, I thought you were suggesting that there was another way</p>



<a name="168438032"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168438032" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168438032">(Jun 18 2019 at 17:48)</a>:</h4>
<p>we described two schemes, yours requires a non-atomic "zero-all-padding" operation and mine does not. both assume <code>freeze</code> and both have to maintain the invariant that the data in memory is not <code>Undef</code></p>



<a name="168438067"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168438067" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168438067">(Jun 18 2019 at 17:49)</a>:</h4>
<p>the reason I am saying this is that this invariant that we both need cannot be upheld if <code>AtomicCell</code> has a <code>get_mut</code> operation</p>



<a name="168438074"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168438074" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168438074">(Jun 18 2019 at 17:49)</a>:</h4>
<p>ah, ok, so you are describing your scheme where you freeze the data before putting it into the AtomicCell</p>



<a name="168438128"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168438128" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168438128">(Jun 18 2019 at 17:49)</a>:</h4>
<blockquote>
<p>ah, ok, so you are describing your scheme where you freeze the data before putting it into the AtomicCell</p>
</blockquote>
<p>I am comparing that to yours, yes. you said there'd be problems because the padding might be different each time you freeze and I explained why that is not a problem. I am not sure if you already accepted that explanation.</p>



<a name="168438211"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168438211" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168438211">(Jun 18 2019 at 17:50)</a>:</h4>
<p>yes, the second iteration of the CAS fixes that, if the CAS is specified to propagate the <del>undef</del> frozen bits properly</p>



<a name="168438216"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168438216" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168438216">(Jun 18 2019 at 17:50)</a>:</h4>
<p>FWIW, that loop that I described already exists, it's at <a href="https://github.com/crossbeam-rs/crossbeam/blob/43967df8182e1f1ce7baebda36361c933caba39b/crossbeam-utils/src/atomic/atomic_cell.rs#L925" target="_blank" title="https://github.com/crossbeam-rs/crossbeam/blob/43967df8182e1f1ce7baebda36361c933caba39b/crossbeam-utils/src/atomic/atomic_cell.rs#L925">https://github.com/crossbeam-rs/crossbeam/blob/43967df8182e1f1ce7baebda36361c933caba39b/crossbeam-utils/src/atomic/atomic_cell.rs#L925</a></p>



<a name="168438232"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168438232" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168438232">(Jun 18 2019 at 17:51)</a>:</h4>
<p><code>CAS</code> would not see any undef... ah yes</p>



<a name="168438238"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168438238" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168438238">(Jun 18 2019 at 17:51)</a>:</h4>
<p>frozen bits are just bits</p>



<a name="168438264"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168438264" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168438264">(Jun 18 2019 at 17:51)</a>:</h4>
<p>but all of this is broken by <code>AtomicCell::get_mut</code> :/</p>



<a name="168438275"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168438275" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168438275">(Jun 18 2019 at 17:51)</a>:</h4>
<p>what's the signature of that ?</p>



<a name="168438287"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168438287" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168438287">(Jun 18 2019 at 17:51)</a>:</h4>
<p><code>&amp;mut AtomicCell&lt;T&gt; -&gt; &amp;mut T</code></p>



<a name="168438363"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168438363" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168438363">(Jun 18 2019 at 17:52)</a>:</h4>
<p>which they had, and then they removed it when I called this out, and then they added it again because the concern is too theoretic</p>



<a name="168438387"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168438387" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168438387">(Jun 18 2019 at 17:52)</a>:</h4>
<p>ah so that allows users to write undef into <code>AtomiCell&lt;T&gt;</code></p>



<a name="168438414"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168438414" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168438414">(Jun 18 2019 at 17:52)</a>:</h4>
<p>(well that's my reading of why they added it again anyway, it was a somewhat heated argument because they claimed it was actually sound... but that's besides the point)</p>



<a name="168438416"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168438416" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168438416">(Jun 18 2019 at 17:52)</a>:</h4>
<p>exactly</p>



<a name="168438422"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168438422" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168438422">(Jun 18 2019 at 17:52)</a>:</h4>
<p>instead of going through the <code>AtomicCell::new(T)</code> constructor, which would <code>freeze</code></p>



<a name="168438424"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168438424" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168438424">(Jun 18 2019 at 17:52)</a>:</h4>
<p>by overwriting the content, re-undef-ing the padding</p>



<a name="168438445"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168438445" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168438445">(Jun 18 2019 at 17:53)</a>:</h4>
<p>yeah so that has to go, i don't think that can ever work</p>



<a name="168438464"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168438464" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168438464">(Jun 18 2019 at 17:53)</a>:</h4>
<p>that's the current status yes. except that that seems entirely silly, this <em>should</em> be possible.</p>



<a name="168438468"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168438468" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168438468">(Jun 18 2019 at 17:53)</a>:</h4>
<p>unless they add a trait bound to <code>AtomicCell&lt;T&gt;</code> to prevent types with padding, which would be an even larger breaking change</p>



<a name="168438473"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168438473" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168438473">(Jun 18 2019 at 17:53)</a>:</h4>
<p>oh and C++ thinks it is possible, but that doesn't mean anything of course^^</p>



<a name="168438542"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168438542" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168438542">(Jun 18 2019 at 17:54)</a>:</h4>
<p>i mean, if you have <code>undef</code> in the <code>T</code> in the atomic cell, your <code>usize</code> will have <code>undef</code> bytes</p>



<a name="168438559"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168438559" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168438559">(Jun 18 2019 at 17:54)</a>:</h4>
<p>so the CAS intrinsic needs to work on <code>undef</code> bits</p>



<a name="168438563"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168438563" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168438563">(Jun 18 2019 at 17:54)</a>:</h4>
<p><a href="https://en.cppreference.com/w/cpp/atomic/atomic/compare_exchange" target="_blank" title="https://en.cppreference.com/w/cpp/atomic/atomic/compare_exchange">https://en.cppreference.com/w/cpp/atomic/atomic/compare_exchange</a> is a delightful read (the "Notes" section in particular)</p>



<a name="168438606"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168438606" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168438606">(Jun 18 2019 at 17:54)</a>:</h4>
<p>yes. that or we have an atomic variant of <code>freeze</code> that does "not count as a data race". whatever the heck that means.</p>



<a name="168438653"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168438653" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168438653">(Jun 18 2019 at 17:55)</a>:</h4>
<p>so i'd prefer to avoid specifying what <code>undef == undef</code> means in the "compare" part of CAS</p>



<a name="168438713"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168438713" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168438713">(Jun 18 2019 at 17:55)</a>:</h4>
<p>well it can only be <code>undef</code> because we can't implement anything else^^</p>



<a name="168438755"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168438755" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168438755">(Jun 18 2019 at 17:55)</a>:</h4>
<p>sure, but they are doing bit equality</p>



<a name="168438771"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168438771" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168438771">(Jun 18 2019 at 17:56)</a>:</h4>
<p>(or UB of course)</p>



<a name="168438858"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168438858" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168438858">(Jun 18 2019 at 17:56)</a>:</h4>
<p>so an <code>atomic_freeze_and_cas</code> intrinsic would also work, but..</p>



<a name="168439024"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168439024" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168439024">(Jun 18 2019 at 17:58)</a>:</h4>
<p>so that C++ section is clear</p>



<a name="168439096"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168439096" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168439096">(Jun 18 2019 at 17:59)</a>:</h4>
<p>"clear?"^^</p>



<a name="168439105"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168439105" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168439105">(Jun 18 2019 at 18:00)</a>:</h4>
<p>an operational semantics is clear</p>



<a name="168439172"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168439172" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168439172">(Jun 18 2019 at 18:00)</a>:</h4>
<p>that's pretty far from one</p>



<a name="168439216"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168439216" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168439216">(Jun 18 2019 at 18:00)</a>:</h4>
<p>i mean, it says that the moment you have padding bits, things might not work, which is also what <code>memcmp</code> says</p>



<a name="168439320"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168439320" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168439320">(Jun 18 2019 at 18:01)</a>:</h4>
<p>"might not work", lol</p>



<a name="168439335"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168439335" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168439335">(Jun 18 2019 at 18:01)</a>:</h4>
<p>AFAIK <code>memcmp</code> is just UB on padding</p>



<a name="168439354"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168439354" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168439354">(Jun 18 2019 at 18:01)</a>:</h4>
<p>i think the same applies to CAS</p>



<a name="168439432"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168439432" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168439432">(Jun 18 2019 at 18:02)</a>:</h4>
<p>well, in my reading of the standard it is (and CERT agrees <a href="https://en.cppreference.com/w/cpp/atomic/atomic/compare_exchange" target="_blank" title="https://en.cppreference.com/w/cpp/atomic/atomic/compare_exchange">https://en.cppreference.com/w/cpp/atomic/atomic/compare_exchange</a> )</p>



<a name="168439475"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168439475" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168439475">(Jun 18 2019 at 18:02)</a>:</h4>
<p>at least in C++, from reading that, i would need to read the standard to be sure, but I don't think the standard specifies the behavior of equality of padding bytes</p>



<a name="168439479"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168439479" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168439479">(Jun 18 2019 at 18:02)</a>:</h4>
<p>CAS dosn't sound like it'd be UB for all padding bytes, it seems to quitw specifically allow being used on structs with padding</p>



<a name="168439503"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168439503" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168439503">(Jun 18 2019 at 18:02)</a>:</h4>
<p>and for unions it doesnt say UB either, it says the value might not converge</p>



<a name="168439530"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168439530" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168439530">(Jun 18 2019 at 18:03)</a>:</h4>
<p>letss see what the standard say, that tone is too loose to be standardese</p>



<a name="168439586"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168439586" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168439586">(Jun 18 2019 at 18:03)</a>:</h4>
<p><a href="http://eel.is/c++draft/atomics.types.generic#atomics.types.operations-17" target="_blank" title="http://eel.is/c++draft/atomics.types.generic#atomics.types.operations-17">http://eel.is/c++draft/atomics.types.generic#atomics.types.operations-17</a></p>



<a name="168439652"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168439652" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168439652">(Jun 18 2019 at 18:04)</a>:</h4>
<p>(magic, I tried to fix the topic)</p>



<a name="168439723"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168439723" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168439723">(Jun 18 2019 at 18:05)</a>:</h4>
<p>that text sounds unimplementable. the value representation does not contain padding, right?</p>



<a name="168439770"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168439770" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168439770">(Jun 18 2019 at 18:05)</a>:</h4>
<p>also <a href="http://eel.is/c++draft/atomics.types.generic#atomics.types.operations-20" target="_blank" title="http://eel.is/c++draft/atomics.types.generic#atomics.types.operations-20">http://eel.is/c++draft/atomics.types.generic#atomics.types.operations-20</a></p>



<a name="168439900"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168439900" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168439900">(Jun 18 2019 at 18:07)</a>:</h4>
<p>and OMG <a href="http://eel.is/c++draft/atomics.types.generic#atomics.types.operations-23" target="_blank" title="http://eel.is/c++draft/atomics.types.generic#atomics.types.operations-23">http://eel.is/c++draft/atomics.types.generic#atomics.types.operations-23</a></p>



<a name="168439968"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168439968" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168439968">(Jun 18 2019 at 18:07)</a>:</h4>
<p><a href="http://eel.is/c++draft/atomics.types.generic#atomics.types.operations-23" target="_blank" title="http://eel.is/c++draft/atomics.types.generic#atomics.types.operations-23">http://eel.is/c++draft/atomics.types.generic#atomics.types.operations-23</a></p>



<a name="168439978"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168439978" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168439978">(Jun 18 2019 at 18:07)</a>:</h4>
<p>how are compilers supposed to implement this? a version of CAS that ignores padding?</p>



<a name="168439981"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168439981" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168439981">(Jun 18 2019 at 18:08)</a>:</h4>
<p>yeah, i was just going to say that</p>



<a name="168440032"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168440032" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168440032">(Jun 18 2019 at 18:08)</a>:</h4>
<p>no idea, I'll ask for clarification</p>



<a name="168440039"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168440039" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168440039">(Jun 18 2019 at 18:08)</a>:</h4>
<p>of course for Rust it'd be even worse due to enums</p>



<a name="168440048"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168440048" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168440048">(Jun 18 2019 at 18:08)</a>:</h4>
<p>that basically says that padding is magically not compared by CAS</p>



<a name="168440059"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168440059" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168440059">(Jun 18 2019 at 18:08)</a>:</h4>
<p>I agree</p>



<a name="168440071"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168440071" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168440071">(Jun 18 2019 at 18:08)</a>:</h4>
<p>i mean, if you have such a magic CAS, then that's obviously the thing to do, but... we don't have those :D</p>



<a name="168440084"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168440084" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168440084">(Jun 18 2019 at 18:08)</a>:</h4>
<p>let's go ask Intel for a CAS with a bitmask :P</p>



<a name="168440105"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168440105" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168440105">(Jun 18 2019 at 18:09)</a>:</h4>
<p>well the bitmask would be good enough for C but not Rust (and only because they hand-wave their way around union)</p>



<a name="168440213"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168440213" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168440213">(Jun 18 2019 at 18:10)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> by making it mutex-atomic.</p>



<a name="168440247"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168440247" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168440247">(Jun 18 2019 at 18:10)</a>:</h4>
<p>well that would still require a way to compare skipping padding</p>



<a name="168440469"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168440469" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168440469">(Jun 18 2019 at 18:13)</a>:</h4>
<p><span class="user-mention" data-user-id="123586">@nagisa</span> looks like a hardware CAS to me: <a href="https://godbolt.org/z/Zj6-Tb" target="_blank" title="https://godbolt.org/z/Zj6-Tb">https://godbolt.org/z/Zj6-Tb</a></p>



<a name="168440486"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168440486" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168440486">(Jun 18 2019 at 18:13)</a>:</h4>
<p>but I also cant really read assembly^^</p>



<a name="168440605"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168440605" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168440605">(Jun 18 2019 at 18:14)</a>:</h4>
<p>clang seems to call a <code>__atomic_compare_exchange</code> thing</p>



<a name="168441247"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168441247" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168441247">(Jun 18 2019 at 18:20)</a>:</h4>
<p><a href="https://cpp.godbolt.org/z/pJX07d" target="_blank" title="https://cpp.godbolt.org/z/pJX07d">https://cpp.godbolt.org/z/pJX07d</a></p>



<a name="168441287"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168441287" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168441287">(Jun 18 2019 at 18:21)</a>:</h4>
<p>so GCC at least just compares padding and calls it a day, but from my understanding of the standard, it is not allowed to do that</p>



<a name="168441980"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168441980" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168441980">(Jun 18 2019 at 18:29)</a>:</h4>
<p>IIUC, the expectation is that C++'s <code>atomic&lt;T&gt;::compare_exchange(T&amp;, T, ..)</code> makes sure that its <code>T</code> has the padding set to some fixed known value, and then on call, it copies its arguments to the stack, sets their padding to the same value, and then does the CAS</p>



<a name="168442007"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168442007" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168442007">(Jun 18 2019 at 18:29)</a>:</h4>
<p><code>atomic&lt;T&gt;</code> does not let you take a reference to its internal <code>T</code>, so it doesn't have to worry about users breaking this invariant <span class="user-mention" data-user-id="120791">@RalfJ</span></p>



<a name="168443094"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168443094" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168443094">(Jun 18 2019 at 18:41)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> so that link above is from C++2a - in C++11, the result of <code>compare_exchange</code> is indeterminate if the type has padding bytes, and they essentially use  two iterations like in Rust</p>



<a name="168444738"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168444738" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168444738">(Jun 18 2019 at 18:58)</a>:</h4>
<p><a href="http://eel.is/c++draft/atomics.ref.generic#3" target="_blank" title="http://eel.is/c++draft/atomics.ref.generic#3">http://eel.is/c++draft/atomics.ref.generic#3</a></p>



<a name="168444838"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168444838" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168444838">(Jun 18 2019 at 18:59)</a>:</h4>
<p><code>std::atomic_ref</code> also works around this, by saying that as long as a <code>atomic_ref</code> to a value is live, you can only access the value through an <code>std::atomic_ref</code>.  This means that the <code>atomic_ref</code> can, on creation, write e.g. <code>0</code> to the padding of the value, and then the <code>compare_exchange</code> assumes that all padding is zero</p>



<a name="168457457"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168457457" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168457457">(Jun 18 2019 at 21:32)</a>:</h4>
<p><a href="https://github.com/crossbeam-rs/crossbeam/pull/372" target="_blank" title="https://github.com/crossbeam-rs/crossbeam/pull/372">https://github.com/crossbeam-rs/crossbeam/pull/372</a> being accepted feels pretty alarming for such an important crate...</p>



<a name="168483776"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168483776" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Vadim Petrochenkov <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168483776">(Jun 19 2019 at 07:35)</a>:</h4>
<blockquote>
<p>that basically says that padding is magically not compared by CAS</p>
</blockquote>
<p>Wait a second, I'm pretty sure I've seen a defect report about that, and it should already be merged into the standard (at least into C++20).</p>



<a name="168483786"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168483786" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Vadim Petrochenkov <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168483786">(Jun 19 2019 at 07:35)</a>:</h4>
<p>Let me check.</p>



<a name="168483982"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168483982" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Vadim Petrochenkov <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168483982">(Jun 19 2019 at 07:38)</a>:</h4>
<p>Ok, apparently the change is there, but it does a different thing than I remembered.</p>



<a name="168484240"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168484240" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Vadim Petrochenkov <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168484240">(Jun 19 2019 at 07:42)</a>:</h4>
<p>The paper - <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0528r3.html" target="_blank" title="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0528r3.html">http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0528r3.html</a>.<br>
(Previous revisions contain some more discussion, not just wording.)</p>



<a name="168493547"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168493547" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168493547">(Jun 19 2019 at 10:09)</a>:</h4>
<p><span class="user-mention" data-user-id="123856">@Vadim Petrochenkov</span> do you have links to those previous revisions?</p>



<a name="168493646"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168493646" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168493646">(Jun 19 2019 at 10:11)</a>:</h4>
<p>but that change sounds like exactly the thing that mandates that padding values be magically ignored</p>



<a name="168493647"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168493647" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168493647">(Jun 19 2019 at 10:11)</a>:</h4>
<p>and I don't see how GCC is doing that</p>



<a name="168493654"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168493654" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168493654">(Jun 19 2019 at 10:11)</a>:</h4>
<p>(and for clang I have no idea what <code>__atomic_compare_exchange</code> does)</p>



<a name="168493718"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168493718" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168493718">(Jun 19 2019 at 10:12)</a>:</h4>
<blockquote>
<p><code>std::atomic_ref</code> also works around this, by saying that as long as a <code>atomic_ref</code> to a value is live, you can only access the value through an <code>std::atomic_ref</code>.  This means that the <code>atomic_ref</code> can, on creation, write e.g. <code>0</code> to the padding of the value, and then the <code>compare_exchange</code> assumes that all padding is zero</p>
</blockquote>
<p>that is interesting. they basically have the "opposite" of our model here -- start "non-atomic" and get a magic "atomic ref" if you also want atomic accesses. in Rust it's the other way around.</p>



<a name="168493734"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168493734" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168493734">(Jun 19 2019 at 10:13)</a>:</h4>
<p>also this excludes things that are otherwise fine in the memory model, such as having a racing atomic and non-atomic access.</p>



<a name="168494148"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168494148" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Vadim Petrochenkov <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168494148">(Jun 19 2019 at 10:21)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> <br>
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0528r2.html" target="_blank" title="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0528r2.html">http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0528r2.html</a><br>
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0528r1.html" target="_blank" title="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0528r1.html">http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0528r1.html</a><br>
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0528r0.html" target="_blank" title="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0528r0.html">http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0528r0.html</a></p>



<a name="168497216"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168497216" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168497216">(Jun 19 2019 at 11:20)</a>:</h4>
<p>thanks!</p>



<a name="168601372"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168601372" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168601372">(Jun 20 2019 at 14:38)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> </p>
<blockquote>
<p>and I don't see how GCC is doing that<br>
(and for clang I have no idea what __atomic_compare_exchange does)</p>
</blockquote>
<p>They don't implement the fixed semantics yet.</p>



<a name="168601391"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168601391" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168601391">(Jun 20 2019 at 14:38)</a>:</h4>
<p>They can implement them by zeroing padding before doing the CAS.</p>



<a name="168601463"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168601463" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168601463">(Jun 20 2019 at 14:39)</a>:</h4>
<p>That is, when you do an <code>atomic&lt;T&gt;::compare_exchange</code>, the padding of all objects involved is always zeroed. While the CAS compares the all bits, the comparison for the padding then does not affect the result.</p>



<a name="168602216"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168602216" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168602216">(Jun 20 2019 at 14:48)</a>:</h4>
<p>This is achieved via the <code>atomic&lt;T&gt;</code> and <code>atomic_ref&lt;T&gt;</code> semantics. </p>
<p><code>atomic&lt;T&gt;</code> is constructed from a <code>T</code>, and owns it - you can only modify its <code>T</code> via the API, there is no <code>get_mut</code>. That is, on construction, the padding is zeroed. When doing <code>atomic&lt;T&gt;::compare_exchange(T&amp;, T, ..)</code> that function moves everything into its stack, zeros the padding, does the CAS, and then writes through the <code>T&amp;</code> back into the user stack frame. </p>
<p><code>atomic_ref&lt;T&gt;</code> is a <code>T*</code>, but the moment you construct it, you can only modify the <code>T</code> it points to via <code>atomic_ref&lt;T&gt;</code> objects (not necessarily the one you created, you can create more,). On construction,  <code>atomic_ref&lt;T&gt;</code> does an atomic load of <code>T</code>, checks if the padding is zeroed, and if so, we are done. Otherwise, it needs to zero the padding, and do an atomic CAS loop, until it can zero the padding of the object it refers to. Once you have your <code>atomic_ref&lt;T&gt;</code> constructed, you know that it points to a <code>T</code> whose padding have been zeroed, and that you can only access it via <code>atomic_ref&lt;T&gt;</code> objects until the last one dies. So on all other operations, like for <code>atomic&lt;T&gt;</code>, you assume that the padding has been zeroed.</p>



<a name="168602434"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168602434" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168602434">(Jun 20 2019 at 14:50)</a>:</h4>
<blockquote>
<p>On construction, atomic_ref&lt;T&gt; does an atomic load of T, checks if the padding is zeroed, and if so, we are done. Otherwise, it needs to zero the padding, and do an atomic CAS loop, until it can zero the padding of the object it refers to.</p>
</blockquote>
<p>This means that constructing an <code>atomic_ref</code> in parallel with doing a non-atomic read of the same location is UB?</p>



<a name="168602496"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168602496" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168602496">(Jun 20 2019 at 14:51)</a>:</h4>
<p>that sounds like a data-race, so probably yes</p>



<a name="168605485"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell%20and%20padding/near/168605485" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/AtomicCell.20and.20padding.html#168605485">(Jun 20 2019 at 15:23)</a>:</h4>
<p>also, depending on the platform, the constructor does not need a CAS loop, e.g., it can just use atomic writes to each padding byte of the pointee, how those overlap with, e.g., larger atomic loads, is platform-dependent</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>